// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

#include "Import/BDFImport/BulkData/Constraint/RBARParser.h"

#include "gtest/gtest.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Constraint/IRBARService.h"

#include "Import/BDFImport/BDFParserBase.h"
using namespace SG::DBManager;
using namespace SG::Import;

using namespace SG::DataStructure::FEM;
extern std::shared_ptr<DBServiceFactory> DB_SERVICEFACTORY;
extern std::vector<std::string>          dividedEntryInfo;

class RBARParserTest : public ::testing::Test
{
  public:
    static void SetUpTestSuite ()
    {
        DB_SERVICEFACTORY->ClearDatabase ();
    }
};

TEST_F (RBARParserTest, ParseEntry1)
{
    std::vector<std::string> Entry = { "RBAR     8231    10285   12785           123456  123456" };
    BDF::divideBulkEntry (Entry, dividedEntryInfo);

    SG::Import::BDF::BULKDATA::ParseRBAR (DB_SERVICEFACTORY, dividedEntryInfo);
    SG::DataStructure::FEM::RBARData data;

    DB_SERVICEFACTORY->get<IRBARService> ()->FindByIndex (0, data);

    ASSERT_TRUE (data.m_id == 8231);

    ASSERT_TRUE (data.m_node1 == 10285);
    ASSERT_TRUE (data.m_node2 == 12785);
    ASSERT_TRUE (data.GetMasterDof1 ().size () == 0);
    ASSERT_TRUE (data.GetMasterDof2 ().size () == 6);
    ASSERT_TRUE (data.GetMasterDof2 ()[0] == SG::DataStructure::FEM::DOFDirections::DOF_Ux);
    ASSERT_TRUE (data.GetMasterDof2 ()[1] == SG::DataStructure::FEM::DOFDirections::DOF_Uy);
    ASSERT_TRUE (data.GetMasterDof2 ()[2] == SG::DataStructure::FEM::DOFDirections::DOF_Uz);
    ASSERT_TRUE (data.GetMasterDof2 ()[3] == SG::DataStructure::FEM::DOFDirections::DOF_Rotx);
    ASSERT_TRUE (data.GetMasterDof2 ()[4] == SG::DataStructure::FEM::DOFDirections::DOF_Roty);
    ASSERT_TRUE (data.GetMasterDof2 ()[5] == SG::DataStructure::FEM::DOFDirections::DOF_Rotz);

    ASSERT_TRUE (data.GetSlaveDof1 ().size () == 6);
    ASSERT_TRUE (data.GetSlaveDof1 ()[0] == SG::DataStructure::FEM::DOFDirections::DOF_Ux);
    ASSERT_TRUE (data.GetSlaveDof1 ()[1] == SG::DataStructure::FEM::DOFDirections::DOF_Uy);
    ASSERT_TRUE (data.GetSlaveDof1 ()[2] == SG::DataStructure::FEM::DOFDirections::DOF_Uz);
    ASSERT_TRUE (data.GetSlaveDof1 ()[3] == SG::DataStructure::FEM::DOFDirections::DOF_Rotx);
    ASSERT_TRUE (data.GetSlaveDof1 ()[4] == SG::DataStructure::FEM::DOFDirections::DOF_Roty);
    ASSERT_TRUE (data.GetSlaveDof1 ()[5] == SG::DataStructure::FEM::DOFDirections::DOF_Rotz);

    ASSERT_TRUE (data.GetSlaveDof2 ().size () == 0);
    ASSERT_TRUE (data.m_alpha == 0);
}

TEST_F (RBARParserTest, ParseEntry2)
{
    std::vector<std::string> Entry = { "RBAR          99       2       3                                " };
    BDF::divideBulkEntry (Entry, dividedEntryInfo);
    SG::Import::BDF::BULKDATA::ParseRBAR (DB_SERVICEFACTORY, dividedEntryInfo);

    SG::DataStructure::FEM::RBARData data;

    DB_SERVICEFACTORY->get<IRBARService> ()->FindById (99, data);

    ASSERT_TRUE (data.m_id == 99);

    ASSERT_TRUE (data.m_node1 == 2);
    ASSERT_TRUE (data.m_node2 == 3);
    ASSERT_TRUE (data.GetMasterDof1 ().size () == 6);
    ASSERT_TRUE (data.GetMasterDof2 ().size () == 0);
    ASSERT_TRUE (data.GetMasterDof1 ()[0] == SG::DataStructure::FEM::DOFDirections::DOF_Ux);
    ASSERT_TRUE (data.GetMasterDof1 ()[1] == SG::DataStructure::FEM::DOFDirections::DOF_Uy);
    ASSERT_TRUE (data.GetMasterDof1 ()[2] == SG::DataStructure::FEM::DOFDirections::DOF_Uz);
    ASSERT_TRUE (data.GetMasterDof1 ()[3] == SG::DataStructure::FEM::DOFDirections::DOF_Rotx);
    ASSERT_TRUE (data.GetMasterDof1 ()[4] == SG::DataStructure::FEM::DOFDirections::DOF_Roty);
    ASSERT_TRUE (data.GetMasterDof1 ()[5] == SG::DataStructure::FEM::DOFDirections::DOF_Rotz);

    ASSERT_TRUE (data.GetSlaveDof2 ().size () == 6);
    ASSERT_TRUE (data.GetSlaveDof2 ()[0] == SG::DataStructure::FEM::DOFDirections::DOF_Ux);
    ASSERT_TRUE (data.GetSlaveDof2 ()[1] == SG::DataStructure::FEM::DOFDirections::DOF_Uy);
    ASSERT_TRUE (data.GetSlaveDof2 ()[2] == SG::DataStructure::FEM::DOFDirections::DOF_Uz);
    ASSERT_TRUE (data.GetSlaveDof2 ()[3] == SG::DataStructure::FEM::DOFDirections::DOF_Rotx);
    ASSERT_TRUE (data.GetSlaveDof2 ()[4] == SG::DataStructure::FEM::DOFDirections::DOF_Roty);
    ASSERT_TRUE (data.GetSlaveDof2 ()[5] == SG::DataStructure::FEM::DOFDirections::DOF_Rotz);

    ASSERT_TRUE (data.GetSlaveDof1 ().size () == 0);
    ASSERT_TRUE (data.m_alpha == 0);
}

TEST_F (RBARParserTest, ParseEntry3)
{
    std::vector<std::string> Entry = { "RBAR          88       2       3    1236      34" };
    BDF::divideBulkEntry (Entry, dividedEntryInfo);
    SG::Import::BDF::BULKDATA::ParseRBAR (DB_SERVICEFACTORY, dividedEntryInfo);

    SG::DataStructure::FEM::RBARData data;
    DB_SERVICEFACTORY->get<IRBARService> ()->FindById (88, data);

    ASSERT_TRUE (data.m_id == 88);

    ASSERT_TRUE (data.m_node1 == 2);
    ASSERT_TRUE (data.m_node2 == 3);
    ASSERT_TRUE (data.GetMasterDof1 ().size () == 4);
    ASSERT_TRUE (data.GetMasterDof1 ()[0] == SG::DataStructure::FEM::DOFDirections::DOF_Ux);
    ASSERT_TRUE (data.GetMasterDof1 ()[1] == SG::DataStructure::FEM::DOFDirections::DOF_Uy);
    ASSERT_TRUE (data.GetMasterDof1 ()[2] == SG::DataStructure::FEM::DOFDirections::DOF_Uz);
    ASSERT_TRUE (data.GetMasterDof1 ()[3] == SG::DataStructure::FEM::DOFDirections::DOF_Rotz);

    ASSERT_TRUE (data.GetMasterDof2 ().size () == 2);
    ASSERT_TRUE (data.GetMasterDof2 ()[0] == SG::DataStructure::FEM::DOFDirections::DOF_Uz);
    ASSERT_TRUE (data.GetMasterDof2 ()[1] == SG::DataStructure::FEM::DOFDirections::DOF_Rotx);

    ASSERT_TRUE (data.GetSlaveDof1 ().size () == 2);
    ASSERT_TRUE (data.GetSlaveDof1 ()[0] == SG::DataStructure::FEM::DOFDirections::DOF_Rotx);
    ASSERT_TRUE (data.GetSlaveDof1 ()[1] == SG::DataStructure::FEM::DOFDirections::DOF_Roty);

    ASSERT_TRUE (data.GetSlaveDof2 ().size () == 4);
    ASSERT_TRUE (data.GetSlaveDof2 ()[0] == SG::DataStructure::FEM::DOFDirections::DOF_Ux);
    ASSERT_TRUE (data.GetSlaveDof2 ()[1] == SG::DataStructure::FEM::DOFDirections::DOF_Uy);
    ASSERT_TRUE (data.GetSlaveDof2 ()[2] == SG::DataStructure::FEM::DOFDirections::DOF_Roty);
    ASSERT_TRUE (data.GetSlaveDof2 ()[3] == SG::DataStructure::FEM::DOFDirections::DOF_Rotz);

    ASSERT_TRUE (data.m_alpha == 0);
}
